package com.colorfruit.admin.business.service.impl;

import com.aliyun.oss.common.utils.DateUtil;
import com.colorfruit.admin.business.dao.ConfigCommonMapper;
import com.colorfruit.admin.business.dao.ReportInfoMapper;
import com.colorfruit.admin.business.service.ReportService;
import com.colorfruit.admin.pojo.constant.CommonConstant;
import com.colorfruit.admin.pojo.entity.ConfigCommon;
import com.colorfruit.admin.pojo.entity.ReportInfo;
import com.colorfruit.admin.pojo.enums.ErrorCodeEnum;
import com.colorfruit.admin.pojo.enums.ReportTypeEnum;
import com.colorfruit.framework.common.util.DataConvertUtil;
import com.colorfruit.framework.common.util.DataValidateUtil;
import com.colorfruit.framework.model.pojo.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {


    @Autowired
    ReportInfoMapper reportInfoMapper;

    @Autowired
    ConfigCommonMapper configCommonMapper;


    @Override
    public BaseResult getReportInfoList() {
        log.info("[service] [IN] editProduct param :");
        List<ReportInfo> reportInfos = reportInfoMapper.selectReportInfoList();
        Iterator iterator = reportInfos.iterator();
        if (iterator.hasNext()){
            ReportInfo reportInfo = (ReportInfo)iterator.next();
            if (reportInfo.getReportType() == ReportTypeEnum.NOT_NEED_REPLACE.getCode()){

            }else if (reportInfo.getReportType() == ReportTypeEnum.REPLACE_WITH_THIS_TABLE.getCode()){
                String model = reportInfo.getModel();
                String replaceData = reportInfo.getReplaceData();
                if (DataValidateUtil.verifyString(replaceData)){
                    String[] dataArray = replaceData.split(",");
                    for (String data : dataArray) {
                        model = model.replaceFirst("_",data );
                    }
                }
                reportInfo.setModel(model);
            }else if (reportInfo.getReportType() == ReportTypeEnum.REPLACE_WITH_OTHER_TABLE.getCode()){
                ConfigCommon configCommonStartTime = configCommonMapper.getConfigInfoByCode(CommonConstant.START_SHOP_TIME);
                Date startTime = DataConvertUtil.StringToDateWithFormat(configCommonStartTime.getConfigValue(), CommonConstant.DATE_FORMAT);
                ConfigCommon configCommonEndTime = configCommonMapper.getConfigInfoByCode(CommonConstant.END_SHOP_TIME);
                Date endTime = DataConvertUtil.StringToDateWithFormat(configCommonEndTime.getConfigValue(), CommonConstant.DATE_FORMAT);

                String startTimeString = DataConvertUtil.dataToStringWithFormat(startTime, CommonConstant.DATA_FORMAT_2);
                String endTimeString = DataConvertUtil.dataToStringWithFormat(endTime, CommonConstant.DATA_FORMAT_2);

                String model = reportInfo.getModel();
                model = model.replaceFirst("_", startTimeString);
                model = model.replaceFirst("_", endTimeString);
                reportInfo.setModel(model);
                log.info("start time = {}, now = {}", startTime, new Date());
                if (startTime.getTime() <= new Date().getTime()){
                    iterator.remove();
                }
            }
        }
        log.info("[service] [OUT] getProductEditInfo success ");
        return new BaseResult(reportInfos);
    }
}
